﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using App.Core.Extensions;
using App.Data;
using System.Data;

public partial class Pages_Admin_ProjectList : BasePage
{
    private const int PROJECT_LIST_SIZE = 25;

    protected void Page_Load(object sender, EventArgs e)
    {
        BindPageInfo();
        if (!IsPostBack)
        {
            //BindProjectList(1);
            BindDropdownList.Customers(ddlCustomer, ConfigReader.DeveloperBusinessTypeID);
            BindDropdownList.PropertyTypes(ddlPropertyType, true);
        }
    }
    protected void BindPageInfo()
    {
        Page.Title = WebUtil.GetPageTitle("Project List");
    }
    protected void BindProjectList(int pageNumber)
    {
        int totalRecord = 0;
        UtilityDAO dao = new UtilityDAO();
        //DbParameter[] parameters = new[] { new DbParameter("@CustomerID", ddlCustomer.SelectedValue) };
        //DataSet ds = dao.GetPagedData(AppSQL.GET_PAGED_PROJECTS_BY_CUSTOMER, parameters, pageNumber, PROJECT_LIST_SIZE, out  totalRecord);
        bool? waitingForApproval = null;
        if(chkIsApproved.Checked)
            waitingForApproval = false;

        DataSet ds = dao.SerchProjectForAdmin(ddlCustomer.SelectedValue.ToInt(), ddlPropertyType.SelectedValue.ToInt(), waitingForApproval, pageNumber, ConfigReader.CommonListPageSize, out totalRecord); 
        ucProjectList.DataSource = ds.Tables[0];
        ucProjectList.EditLink = AppConstants.Pages.PROJECT_LIST_BY_CUSTOMER + "?" + AppConstants.QueryString.ID + "={0}";
        //ucProjectList.DeleteLink = Request.Url.AbsolutePath + "?" + AppConstants.QueryString.ID + "={0}&" + AppConstants.QueryString.DELETE + "=True";
        ucProjectList.LinkFields = "ID";
        ucProjectList.DataBind();

        ///BindPager
        ucProjectListPager.TotalRecord = totalRecord;
        ucProjectListPager.PageNo = pageNumber;
        ucProjectListPager.PageSize = ConfigReader.CommonListPageSize; //PROJECT_LIST_SIZE;
        ucProjectListPager.TotalRecordMessage = "Total {0} Project(s) found.";
        ucProjectListPager.DataBind();

        //Bind Action Buttons
        btnApprove.Visible = btnMakeHot.Visible = btnRemoveHot.Visible = totalRecord > 0;
        
    }
    protected void ApproveSelectedProjects()
    {
        String selectedIDs = Request.Form[ucProjectList.SelectionCheckboxName];
        if (!selectedIDs.IsNullOrEmpty())
        {
            List<int> ids = new List<int>();
            foreach (String id in selectedIDs.Split(','))
                ids.Add(id.ToInt());

            if (ids.Count > 0)
            {
                BtcHousingEntities context = new BtcHousingEntities();
                var projects = from P in context.Projects where ids.Contains(P.ID) select P;
                foreach (Project project in projects)
                {
                    project.IsApproved = true;
                    project.Modified = DateTime.Now;
                    project.ModifiedBy = SessionCache.CurrentUser.ID;
                }
                context.SaveChanges();
            }
        }
    }
    protected void MakeSelectedHot(bool makeFeatured)
    {
        String selectedIDs = Request.Form[ucProjectList.SelectionCheckboxName];
        if (!selectedIDs.IsNullOrEmpty())
        {
            List<int> ids = new List<int>();
            foreach (String id in selectedIDs.Split(','))            
                ids.Add(id.ToInt());

            if (ids.Count > 0)
            {
                BtcHousingEntities context = new BtcHousingEntities();
                var projects = from P in context.Projects where ids.Contains(P.ID) select P;
                foreach (Project project in projects)
                {
                    project.IsHot = makeFeatured;
                    project.Modified = DateTime.Now;
                    project.ModifiedBy = SessionCache.CurrentUser.ID;
                }
                context.SaveChanges();
            }
        }
    }
    #region Event Handlers
    protected void btnApprove_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            ApproveSelectedProjects();
            BindProjectList(1);
        }
    }
    protected void btnMakeHot_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            MakeSelectedHot(true);
            BindProjectList(1);
        }
    }
    protected void btnRemoveHot_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            MakeSelectedHot(false);
            BindProjectList(1);
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        //if (!ddlCustomer.SelectedValue.IsNullOrEmpty())
        {
            BindProjectList(1);
        }
    }
    protected void ucProjectListPager_PageIndexChanged(object sender, PagerEventArgs e)
    {
        BindProjectList(e.PageIndex);
    }
    #endregion Event Handlers
}